import Expand from './assist/expand';
export default class BaseNode extends zrender.Group {
    constructor(data) {
        super();
        this.default = { //默认设置
            id: (new Date().getTime()%100000000),
            text: '节点文字',
            textPadding: [8, 10, 8, 10], //表示 上右下左 偏移的距离,一定要写全
            textFill: '#666',
            transformText: true,
            fontSize: 12,
            textBorderRadius: 3,
            fontFamily: 'Microsoft YaHei',
            fontStyle: '',
            fontWeight: ''
        };

        this.data = {
            ...this.default,
            ...data
        };
        this.childrens = []; //用于存储子元素
        this._parent = null; //父节点
        this.visible = true;
        this.selected = false;
        this.minder = null; //该节点所属的导图
        this.induce = null;  //总结
        this.wireFrame=null;
        this.expandElement=new Expand(this);
        this.renderText();
        this.initEvent();
    }
    //初始化函数
    renderText() {
        this.data.textLineHeight = parseInt(this.data.fontSize) + 2; //行高比字体大一点
        if (this.text) {
            this.text.attr({
                style: this.data
            });
        } else {
            this.text = new zrender.Text({
                style: this.data,
                z: 10
            });
            this.add(this.text);
            this.add(this.expandElement.g);
            this.nodeData = this;
        }

        this.setExpandPosition();

        if (this.selected) {
            this.select();
        }
    }
    //获取id
    getId() {
		var id = this.data.id || this.id;
		console.log("BaseNode.getId:"+parseInt(id));
        return this.data.id || this.id;
    }
	getText() {
	    return this.data.text || this.text;
	}
    //设置minder
    setMinder(m) {
        this.minder = m;
    }
    //获取minder
    getMinder() {
        return this.minder;
    }
    //设置父节点
    setParent(node) {
        this._parent = node;
    }
    //获取父节
    getParent() {
        return this._parent;
    }
    //设置属性值
    setData(k, v) {
        this.clearOffset();
        if (zrender.util.isObject(k)) {
            this.data = {
                ...this.data,
                ...k
            }
        } else if (zrender.util.isString(k)) {
            this.data[k] = v;
        }
        this.refresh();
    }
    //获取属性值
    getData(k) {
        return k ? this.data[k] : this.data;
    }
    //显示
    // show() {
    //     this.visible = true;
    //     this.show()
    // }
    // //隐藏
    // hide() {
    //     this.visible = false;
    //     this.hide();
    // }
    //选中
    select() {
        this.selected = true;
        this.text.setStyle('textBackgroundColor', 'yellow');
    }
    //取消选中
    unSelect() {
        this.selected = false;
        this.text.setStyle('textBackgroundColor', this.data.textBackgroundColor || 'transparent');
    }
    //是否是叶子节点
    isLeaf() {
        return this.childrens.length == 0;
    }
    //获取子节点
    getChildren() {
        return this.childrens;
    }
    //新增子节点
    addChild(node, index) {
        //index指定插入的位置
        node._parent = this;
        if (typeof (index) == 'number') {
            this.childrens.splice(index, 0, node); //插入至指定位置
        } else {
            this.childrens.push(node);
        }
    }
    //删除节点
    removeChild(node) {
        var index = this.childrens.indexOf(node);
        index > -1 && this.childrens.splice(index, 1);
        return index;
    }
    //获取同级节点
    getSiblings() {
        var sib = [];
        if (this._parent) {
            this._parent.childrens.forEach((item) => {
                if (item != this) {
                    sib.push(item);
                }
            });
        }
        return sib;
    }
    //获取节点层级
    getLevel() {
        var level = 0;
        var parent = this._parent;
        while (parent) {
            level++;
            parent = parent.getParent();
        }
        return level;
    }
    //更新方法
    refresh() {
        this.renderText();
    }
    //设置节点位置
    setPosition(x, y) {
        this.attr({
            position: [x, y]
        });
    }
    //获取节点位置
    getPosition() {
        var pos = this.position.slice();
        return {
            x: pos[0],
            y: pos[1]
        }
    }
    //移动位置
    move(dx, dy) {
        var p = this.getPosition();
        this.setPosition(p.x + dx, p.y + dy);
    }

    //获取所有子孙节点
    getNodeList() {
        var list = [];
        (function getList(node) {
            list.push(node);
            node.childrens.forEach((n) => {
                getList(n);
            });
        })(this);
        return list;
    }

    //设置expand位置
    setExpandPosition(){
         var textBox=this.text.getBoundingRect();
         var expandBox=this.expandElement.getBox();
         if(this.direct=='right'){
             this.expandElement.setPosition(textBox.width+4,textBox.height-expandBox.height/2);
         }else{
             this.expandElement.setPosition(-16,textBox.height-expandBox.height/2);
         }
    }

    //展开方法
    expand(){
        this.expandElement.expand();
        (function getList(node) {
            node.show();
            if(node.induce){
                node.induce.show()
            }
            if(node.isExpand()){
                node.childrens.forEach((n) => {
                    getList(n);
                });
            }
            
        })(this);
    }

    //收缩方法
    collapse(){
        this.expandElement.collapse();
        this.getNodeList().forEach((item)=>{
                if(item!=this){
                    item.hide();
                    if(item.induce){
                        item.induce.hide();
                    }
                }
        });
    }
    //是否展开
    isExpand(){
        return this.expandElement.isExpand();
    }
    //获取文字包围盒
    getTextBox(){
        return this.text.getBoundingRect();
    }

    initEvent() {
        this.text.on('click', () => {
            this.getMinder().clearSelectNode();
            this.select();
            this.__zr.trigger('selectNode', {
                node: this
            });
        });
        this.text.on('dblclick', () => {
            this.__zr.trigger('editNode', {
                node: this
            });
        });
    }
}